 /**
  * 显示合同列表
  */
  function showconpage(){
	  parent.showiframepage({"checked":1,"children":[],"createdate":"","endsize":0,"href":"/customer/intocontractlist","icon":"fa-navbar0","id":15,"isCurrent":1,"pagerFlag":0,"pid":2,"rows":0,"rowsCount":0,"spread":1,"timeLine":"","title":"合同管理","totalResults":0,"updatedate":""});
  }
  
  function submitForm(){
	  $("#hetongform").submit();
  }

 
  
    layui.use(['icheck','element',"laytpl",'laypage','form','laydate'], function() {
        var $ = layui.jquery,
        layer = layui.layer,
        laytpl = layui.laytpl,
        laypage = layui.laypage;
        var form = layui.form();
        var tjstate =0;
      
   
 
	    //更新渲染  
	    form.render(); 
	    
	   
    	 getZsUser = function (o){
    		 var result ="";
    		  var values = o;
    			$.ajax({
        			type : "post",
        			url : "/customer/userlist",
        			data : values,
        			async:false,
        			dataType : "json",
        			success : function(data){
        				result = data;
        				//layer.msg('资源获取到线索库成功', {icon: 1},function(){
       					// window.location.href=url;
    					// });
        			}
        		});
    	  	 return result
    	 }
    	 var rzjgtitle ="人才证书价格";
    	 var qyjgtitle ="企业证书价格";
    	 var zssl="备注";
    	 var rytitle="猎证";
    	 form.on('select(contracttype)', function(data){
    		  if(data.value==0){
   		       //资质合同	 
   			   window.location.href="/customer/contracttemplate?type=0&cusid="+$("#qyid").val();
   		    }else  if(data.value==1){
   		    	//猎证合同  
   		    	 
   		    }else if (data.value==2){
   		    	//培训合同 
   		        window.location.href="/customer/contracttemplate?type=2&cusid="+$("#qyid").val();
   		    }
    	  });
    	 
    	 
    	 //合同表单
    	 $("#hetongform").validate({
         	rules: {
         	 
 	    		signingtime : {
 	    			required:true
 	    		},
 	    		totalamount : {
 	    			required:true,
 	    			digits:true
 	    		},
 	    		contracttype : {
 	    			required:true
 	    		},
 	    		lifetype : {
 	    			required:true
 	    		}
 	    	},
 	    	messages: {
 	    		 
 	    		signingtime : {
 	    			required:"请选择合同签订时间"
 	    		},
 	    		totalamount : {
 	    			required:"添加人员后会自动计算合同金额",
 	    			digits:"请填写正确金额"
 	    		},
 	    		contracttype : {
 	    			required:"请选择合同类型"
 	    		},
 	    		lifetype : {
 	    			required:"请选择合同签订年限"
 	    		}
 	    	},
 	    	errorPlacement: function(error, element) {
 	    		 tjstate =0;
 	    		parent.chanagestate();
 				layer.tips(error.text(), element, {
 					tips: [2,tipColor],
 	     		 tipsMore: true
 	     		});
 	   		}, 
	 	   	 submitHandler: function(formobj) { 
	 	     	
           		  if(validatezstj()){
           		   tjstate =1;
           			var values = $("#hetongform").serialize()
           			$.ajax({
         		 		type : "post",
         		 		url : "/customer/savecontract",
         		 		data : values,
         		 		dataType : "json",
         		 		async : false,
         		 		success : function(data){
         		 			if(data==1){
         		 				layer.closeAll();
         		 				layer.msg('合同申请成功', {icon: 1,shade: 0.3},function(){
         		 					layer.closeAll();
         		 					parent.layer.confirm('继续添加回款信息?', function(index){
               		 				  //do something
               		 					parent.layer.closeAll();
               		 				   parent.showconpage();
               		 				  layer.close(index);
               		 				} , function(index){
               		 				    parent.layer.closeAll();
               		 			        layer.close(index);
               		 				}
         		 				  ); 
         		 					
         		 				});
         		 			 
         		 				
         		 			   
         		 			}else{
         		 				layer.msg('合同申请失败', {icon: 2});
         		 			}
         		 		}
         		 	});
           			  
           		  }else{
           			parent.chanagestate();
           		  }   
	 		      return  false;
	 	      }
         	
         });
    	 //验证证书相关条件 在这计算服务费用和合同总金额
    	 var validatezstj = function(){
    		 var result =true;
    		 var trs = $(".tb_zs tr");
    		 var resallcount = 0; //人才证书总价
    		 var cusallcount = 0; //企业证书总价
    		 var othallcount = 0; //其他价格总价
    		 
    		 if(trs.length<1){ //判断是否已添加证书
    			 layer.tips("请选择证书", $("#sxzs"), {
    				 tips: [1,tipColor],
    				 tipsMore: true
    			 });
    			 return false;
    		 }
    		 	
    		 var inputobj = trs.find("input[name=totalms]"); //人才
    		 var inputobj2 = trs.find("input[name=alonems]"); //企业
    		 var inputobj3 = trs.find("input[name=otmoney]"); //其他
    		 
    		 for(var i=0;i<inputobj.length;i++){
    			 //判断证书价格是否已填
    			 var val = $(inputobj[i]).val();
    			 if(val.length<1){
    				 layer.tips("请填写"+rzjgtitle, $(inputobj[i]), {
    					 tips: [2,tipColor],
    					 tipsMore: true
    				 });
    				 result = false;
    			 }else{
    				 var reg = /^[0-9]*$/;
    				 if (!reg.test(val)) { 
    					 layer.tips("价格请填写数字", $(inputobj[i]), {
    						 tips: [2,tipColor],
    						 tipsMore: true
    					 });
    					 result = false;
      				}else{
      					resallcount += parseInt(val);
      				}
      			}
      			
      		  }
      		for(var i=0;i<inputobj2.length;i++){
    			  //判断证书价格是否已填
    			var val = $(inputobj2[i]).val();
    			if(val.length<1){
    				 layer.tips("请填写"+rzjgtitle, $(inputobj2[i]), {
        				 tips: [2,tipColor],
	           	     		 tipsMore: true
	           	     	 });
    				result = false;
    			}else{
    				var reg = /^[0-9]*$/;
    				if (!reg.test(val)) { 
    					layer.tips("价格请填写数字", $(inputobj2[i]), {
           				 tips: [2,tipColor],
 	           	     		 tipsMore: true
 	           	     	 });
    					result = false;
    				} else {
    					cusallcount += parseInt(val);
    				}
    			} 
      		}
      		for (var i = 0;i < inputobj3.length;i ++) {
      			var val = $(inputobj3[i]).val();
      			var reg = /^[0-9]*$/;
      			if (!reg.test(val)) {
      				layer.tips("价格请填写数字", $(inputobj2[i]),{
      					tips: [2,tipColor],
      					tipsMore: true
      				});
      				result = false;
      			} else if (val.length > 0) {
      				othallcount += parseInt(val);
      			}
      		}
      		
  			if(cusallcount<(resallcount+othallcount)){
  				layer.tips(rzjgtitle+"不能大于"+qyjgtitle, $("#sxzs"), {
  					tips: [1,tipColor],
  					tipsMore: true
  				});
  				result = false;
  			}else{
  				//服务费用=企业-人才-其他
  				$("#servicecharge").val(cusallcount-resallcount-othallcount);
  				//合同总价=企业
  				$("#totalamount").val(cusallcount);
  			}
      		return result;
    	 }
    	 
    	 
    	 $(document).on("blur","input[name=alonems]",function(){
    		 validatezstj();
    	 })
    	 $(document).on("blur","input[name=totalms]",function(){
    		 validatezstj();
    	 })
     	 
     	 var zspop = "";
     	 //添加证书
          $('.zhengshu').on('click',function(){
        	  zspop = layer.open({
           		title:'添加证书',
           	 	type:1,
           	 	id:"tjzs",
              	  	shadeClose: true,
              	  	content:$('#TM-layui-zhengshu'),
                   btnAlign:'c',
                   area:['90%','95%']
              	}); 
           }); 
    	 
    	 //证书添加
    	 $(document).on('click','.addzs',function(){
    		  var result = eval('(' + $(this).attr("data") + ')') ,
    		  getTpl = $("#zs").html();
    		  if($(".tb_zs  tr[data="+result.id+"]").length>0){
    			  layer.msg('请勿重复添加' ,{icon: 0});
    			  return false;
    		  }
    		  laytpl(getTpl).render(result, function(html){
 				 $(".tb_zs").append(html);
 				layer.close(zspop);
 					  //view.innerHTML = html;
 			  });
    	 });
    	 
    	 //证书删除
    	 $(document).on('click','.deletezs',function(){
    		var id = $(this).attr("data");
    		   layer.confirm('确定要删除该证书吗？', {
    			  btn: ['确定','取消'] //按钮
    			}, function(){
    			   $(".zs"+id).remove(); 
    			   layer.msg("删除成功",{icon:1 });
    			}, function(){}); 
    	 });
    	 //证书其它费用
    	 $(document).on('click','.addothermoney',function(){
    		var id = $(this).attr("data");
    		// $("#othermy"+id).show(); 
    		 $("#othermy"+id).toggle();
    		 $("#othermy"+id +"  input[name=otmoney]").val(0);
    	 });
    	 
    	//证书搜索
    	$(".zssearchbtn").on("click",function(){
    		var kd =  $.trim($("#zskeyword").val());
    		$("#skpg").val(1);
    		if(kd.length<1){
    			
    			layer.tips("请输入关键字", "#zskeyword", {
    				tips: [1,'#f7625e'], 
    				time:5000,
         		    tipsMore: false
         		});
    			return false;
    		}else{
    			$("#zskeyword").val(kd);
    		    var values = $("#zssearch").serialize()
    			var result = getZsUser(values);
    		    var getTpl = $("#zslb").html();
    		    
				 laytpl(getTpl).render(result, function(html){
				 $(".tb_customerzs").html(html);
					  //view.innerHTML = html;
				 });
    			 laypage({
                      cont: 'zspage',
                      curr: 1//'${pager.pageNo}'
                      ,pages: result.totalPages//'${pager.totalPages}' //总页数
                      ,
                      groups: 5 //连续显示分页数
                      ,
                      skip: true,
                      jump: function(obj, first) {
                          //得到了当前页，用于向服务端请求对应数据
                          var curr = obj.curr;
                          if(!first) {
                        	  $("#skpg").val(curr);
                        	   values = $("#zssearch").serialize()
                  			   result = getZsUser(values); 
                        	   var getTpl = $("#zslb").html();
                        	   laytpl(getTpl).render(result, function(html){ 
                					 $(".tb_customerzs").html(html); 
                			    }); 
                          }
                      }
                  });  
    		}
    	})
    	
    	$(".searchbtnclean").on("click",function(){
    		$("#keyWord").val("");
    		$("#province").val("");
    		$("#address").val("");
    		$("#city").html("<option value=\"\">请选择市</option>");
		    $("#city").val("");
		    $("#category").val("");
		    $("#zizhiType").val("");
    	});
    	 
    	
    	//全选，取消全选
    	$("#selected-all").on("click",function(){
			if ($("#selected-all").is(":checked")) {
				$("[name='customcheckbox']").prop("checked",true);
			} else {
				$("[name='customcheckbox']").prop("checked",false);
			}
		});
    	
 
    });
	
 

	//子页面调用 客户详情
	function infoShow(id){
		var url = "/customer/customerinfo1/c"+id;
		parent.layer.open({
			  	type:2,
	  		title:"企业详情",
	  		shadeClose:true,
	  		content: url,
	  		area:['80%','95%'],
	  		maxWidth:1000,
			});
	};	
